﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Tyshka.Subroutines.ViewModels;

namespace Tyshka.Subroutines.Infrastructure
{
    public static class CustomHtmlHelpers
    {
        public static void AddInnerTag(this TagBuilder container, TagBuilder inner)
        {
            container.InnerHtml += inner.ToString();
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id)
        {
            return html.TextInput(label, id, (Dictionary<String, String>)null);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, String comment)
        {
            return html.TextInput(label, id, comment, String.Empty, InputTypes.Text, (Dictionary<String, String>) null);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, String comment, String value)
        {
            return html.TextInput(label, id, comment, value, InputTypes.Text, (Dictionary<String, String>)null);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, Dictionary<String, String> errors)
        {
            return html.TextInput(label, id, InputTypes.Text, errors);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, InputTypes inputType)
        {
            return html.TextInput(label, id, inputType, null);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, InputTypes inputType, Dictionary<String, String> errors)
        {
            return html.TextInput(label, id, String.Empty, String.Empty, inputType, errors);
        }

        public static MvcHtmlString TextInput(this HtmlHelper html, String label, String id, String comment, String value, InputTypes inputType, Dictionary<String, String> errors)
        {
            bool isValidationError = errors != null && errors.ContainsKey(id);

            TagBuilder outerContainer = new TagBuilder("div");
            outerContainer.AddCssClass("control-group");

            if (isValidationError)
            {
                outerContainer.AddCssClass("error");
            }

            TagBuilder labelContainer = new TagBuilder("label");
            labelContainer.AddCssClass("control-label");
            labelContainer.MergeAttribute("for", id);
            labelContainer.SetInnerText(label);

            outerContainer.InnerHtml += labelContainer.ToString();

            TagBuilder innerContainer = new TagBuilder("div");
            innerContainer.AddCssClass("controls");

            TagBuilder input = new TagBuilder("input");
            input.MergeAttribute("type", inputType.ToString());
            input.AddCssClass("input-xlarge");
            input.MergeAttribute("name", id);
            input.MergeAttribute("id", id);

            if (!String.IsNullOrEmpty(value))
            {
                input.MergeAttribute("value", value);
            }
            innerContainer.InnerHtml += input.ToString();

            if (isValidationError)
            {
                TagBuilder span = new TagBuilder("span");
                span.AddCssClass("help-inline");
                span.SetInnerText(errors[id]);
                innerContainer.InnerHtml += span.ToString();
            }

            if (!isValidationError && !String.IsNullOrEmpty(comment))
            {
                TagBuilder span = new TagBuilder("span");
                span.AddCssClass("help-inline");
                span.SetInnerText(comment);
                innerContainer.InnerHtml += span.ToString();
            }

            outerContainer.InnerHtml += innerContainer.ToString();

            return new MvcHtmlString(outerContainer.ToString());
        }

        public static MvcHtmlString StackParameterEditRow(this HtmlHelper html, int offset, String comment)
        {
            //<tr>
			//<td>[SP]</td>
		    //<td><input type="Text" class="input-xlarge"></td>
			//</tr>

            TagBuilder row = new TagBuilder("tr");

            var postfix = String.Empty;
            if (offset < 0)
            {
                postfix = offset.ToString();
            }

            if (offset > 0)
            {
                postfix = String.Format("+{0}", offset);
            }

            var firstCell = new TagBuilder("td");
            firstCell.SetInnerText(String.Format("[SP{0}]", postfix));
            row.AddInnerTag(firstCell);

            var secondCell = new TagBuilder("td");
            var input = new TagBuilder("input");
            input.MergeAttribute("type", "text");
            input.AddCssClass("input-xlarge");
            input.MergeAttribute("name",String.Format("input_stack_{0}", offset) );
            input.MergeAttribute("id", String.Format("input_stack_{0}", offset));

            if (!String.IsNullOrEmpty(comment))
            {
                input.MergeAttribute("value", comment);
            }

            secondCell.AddInnerTag(input);
            row.AddInnerTag(secondCell);

            return new MvcHtmlString(row.ToString());
        }

        public static MvcHtmlString InputRegisterRow(this HtmlHelper html, InputRegisterDataWrapper register)
        {
            return html.RegisterRow(
                register,
                r => String.Format("input_used_{0}", r.RegisterId),
                r => String.Format("input_text_{0}", r.RegisterId)
                );
        }

        public static MvcHtmlString OutputRegisterRow(this HtmlHelper html, InputRegisterDataWrapper register)
        {
            return html.RegisterRow(
                register,
                r => String.Format("output_used_{0}", r.RegisterId),
                r => String.Format("output_text_{0}", r.RegisterId)
                );
        }

        public static MvcHtmlString RegisterRow(this HtmlHelper html, 
                    InputRegisterDataWrapper register, 
                    Func<InputRegisterDataWrapper, String> checkboxId, 
                    Func<InputRegisterDataWrapper, String> inputId)
        {
            TagBuilder row = new TagBuilder("tr");

            //<td><input type="checkbox" id="input_used_1" name="input_used_1"></td>
            TagBuilder firstCell = new TagBuilder("td");
            TagBuilder checkbox = new TagBuilder("input");
            checkbox.MergeAttribute("type", "checkbox");
            checkbox.MergeAttribute("id", checkboxId(register));
            checkbox.MergeAttribute("name", checkboxId(register));
            if (register.IsUsed)
            {
                checkbox.MergeAttribute("checked", String.Empty);
            }
            firstCell.InnerHtml += checkbox.ToString();
            row.InnerHtml += firstCell.ToString();

            //<td>A</td>
            TagBuilder secondCell = new TagBuilder("td");
            var badge = new TagBuilder("span");
            badge.AddCssClass("badge");
            badge.AddCssClass("badge-inverse");
            badge.SetInnerText(register.Name);
            secondCell.InnerHtml += badge.ToString();
            row.InnerHtml += secondCell.ToString();
            
            //<td><input type="Text" class="input-xlarge"></td>
            TagBuilder thirdCell = new TagBuilder("td");
            var input = new TagBuilder("input");
            input.MergeAttribute("type", "text");
            input.AddCssClass("input_xlarge");
            input.MergeAttribute("id", inputId(register));
            input.MergeAttribute("name", inputId(register));
            if (!String.IsNullOrEmpty(register.Comment))
            {
                input.MergeAttribute("value", register.Comment);
            }

            thirdCell.InnerHtml += input.ToString();
            row.InnerHtml += thirdCell.ToString();

            return new MvcHtmlString(row.ToString());
        }

    }
}